﻿Imports System.Runtime.CompilerServices
Imports System.Windows.Media.Imaging
Imports Microsoft.Phone.Controls

Namespace Extension
    Public Module PhoneApplicationPageExtensionMethods
        <Extension>
        Public Function CanSetScreenCaptureEnabled(page As PhoneApplicationPage) As Boolean
            Return Environment.OSVersion.Version >= New Version(8, 0, 10322)
        End Function

        <Extension>
        Public Sub SetScreenCaptureEnabled(page As PhoneApplicationPage, enabled As Boolean)
            Dim propertyInfo = GetType(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled")

            If propertyInfo Is Nothing Then
                Throw New NotSupportedException("Not supported in this Windows Phone version!")
            End If

            propertyInfo.SetValue(page, enabled)
        End Sub

        <Extension>
        Public Function GetScreenCaptureEnabled(page As PhoneApplicationPage) As Boolean
            Dim propertyInfo = GetType(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled")

            If propertyInfo Is Nothing Then
                Throw New NotSupportedException("Not supported in this Windows Phone version!")
            End If

            Return propertyInfo.GetValue(page)
        End Function

        <Extension>
        Public Function RenderControlAsImage(element As FrameworkElement) As WriteableBitmap
            element.UpdateLayout()
            element.Measure(New Size(element.Width, element.Height))
            element.Arrange(New Rect(0, 0, element.Width, element.Height))
            Return New WriteableBitmap(element, Nothing)
        End Function
    End Module
End Namespace